using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Decav.Gatsb.Mobile;
using Decav.Gatsb.Configuration;
using Decav.Reality;

namespace Decav.Gatsb.Application.CommandWorkflow
{
    /// <summary>
    /// Authenticates a user based on a mobile message that he sends.
    /// </summary>
	public partial class AuthenticateMobileMessageActivity: SequenceActivity
	{
		public AuthenticateMobileMessageActivity()
		{
			InitializeComponent();
		}

        /// <summary>
        /// The dependency property that gets or sets the MobileMessage that will be posted to Gatsb.
        /// </summary>
        public static DependencyProperty InputMessageProperty = DependencyProperty.Register(
            "InputMessage", typeof(MobileMessage), typeof(AuthenticateMobileMessageActivity));

        /// <summary>
        /// Gets or sets the MobileMessage that will be posted to Gatsb.
        /// </summary>
        [ValidationOption(ValidationOption.Required)]
        public MobileMessage InputMessage
        {
            get { return (MobileMessage)GetValue(InputMessageProperty); }
            set { SetValue(InputMessageProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets if the authentication is successful.
        /// </summary>
        public static DependencyProperty AuthenticationSuccessfulProperty = DependencyProperty.Register(
            "AuthenticationSuccessful", typeof(bool), typeof(AuthenticateMobileMessageActivity));

        /// <summary>
        /// Gets or sets if the authentication is successful.
        /// </summary>
        public bool AuthenticationSuccessful
        {
            get { return (bool)GetValue(AuthenticationSuccessfulProperty); }
            set { SetValue(AuthenticationSuccessfulProperty, value); }
        }
        

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            // Attempt to authenticate
            try
            {
                GatsbServiceApplication.CreateNewThreadApplication(InputMessage.SendingPhoneNumber, InputMessage.SendingAddress);
                InputMessage.SendingPhoneNumber = GatsbApplication.Current.CurrentUser.MobilePhoneNumber;
                AuthenticationSuccessful = true;
            }
            catch(AuthenticationException)
            {
                AuthenticationSuccessful = false;
            }
        }

	}
}
